Skip to content

fix: close a Codex trace when Codex exits, not at the next session - #80

Open
baharedr wants to merge 1 commit into
mainfrom
codex-close-on-exit
Open

baharedr wants to merge 1 commit into
mainfrom
codex-close-on-exit

Conversation

@baharedr

Copy link
Copy Markdown

Why

Codex has no SessionEnd, so a Codex trace is closed only by the stale sweep. The sweep runs at the next Codex session start, at least an hour after the session was last seen, and sends spans that end at that last-seen time.

The Rius receiver now refuses a span it receives more than an hour after the span ended (glassflow/argus-core#677). Every Codex closing span arrives later than that, so every Codex root span would stay pending for good, and the trace never counts as finished.

What

  • Pinger: the heartbeat pinger already watches the Codex process. When it sees Codex exit, it now closes the trace at once through exporter.close_exited_codex.
  • Rollout first: the close reads what the rollout holds past the last hook before closing. A Stop export that hasn't taken the session lock yet loses nothing; the close waits up to 30s for a Stop export that holds it.
  • End time: the close ends every open span at the exit, the same way a Claude Code trace ends at its SessionEnd.
  • Key check: it only touches traces opened with the key in force, as the sweep does.
  • Sweep stays: it is still the fallback for a session no pinger watched, such as an unidentified Codex process or one running past the pinger's 12h cap.
  • Claude Code and Cursor: unchanged.

Tests

  • New tests:
    • The trace closes at the exit time, not the last-seen time.
    • Rollout lines past the last hook are sent.
    • A trace opened with another key is left alone.
    • A closed trace sends nothing more.
    • The pinger runs its exit hook only when the watched process dies.
    • Only Codex gets an exit hook.
  • Catches the bug: I mutated the close to use the last-seen time, then to skip the rollout read. Each mutation fails its test.
  • End to end: I ran a real heartbeat.py --agent codex process watching a stand-in Codex process, against a local OTLP receiver, after a real Stop export.
    • After killing the process, the closing codex session and subagent roots arrived immediately, ending 0.08s after the exit.
    • On main, nothing is sent and the trace stays open.
  • Full suite: python -m pytest -q on 3.12 gives 2176 passed, 1 skipped.

Codex has no SessionEnd, so a Codex trace was only ever closed by the
stale sweep: at the next Codex session start, at least an hour after the
session was last seen, with spans that end then. The Rius receiver now
refuses a span it gets more than an hour after its end (argus-core #677),
so every Codex root would stay pending for good.

The heartbeat pinger already watches the Codex process. When it sees it
exit, it now closes the trace at once (exporter.close_exited_codex): it
reads what the rollout holds past the last hook, so a Stop export that has
not taken the lock yet loses nothing, then closes every open span ending at
the exit, as a Claude Code trace ends at its SessionEnd. Only traces opened
with the key in force are touched, as in the sweep. The sweep stays for a
session no pinger watched.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant